package bepviet.android.ui;

import java.util.ArrayList;

import bepviet.android.engine.DataAdapter;
import bepviet.android.engine.MyArrayAdapter;
import bepviet.android.engine.R;
import bepviet.android.properties.StringInPair;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListChonMonAn {

	private Activity mcontext;
	private String loai, column;
	private int ma;

	public ListChonMonAn(Activity context, int ma, String loai, String column) {
		this.mcontext = context;
		this.ma = ma;
		this.loai = loai;
		this.column = column;
	}
	
	private LinearLayout layoutmain;
	private DataAdapter dataAdpater;
	private TextView txtback, txttitle;
	private MyArrayAdapter adapter;
	private ArrayList<StringInPair> array_list;
	private ListView listview;

	public View getView() {

		View view = LayoutInflater.from(mcontext).inflate(R.layout.list_food,
				null);
		array_list = new ArrayList<StringInPair>();
		listview = (ListView) view.findViewById(R.id.listMonAn);
		layoutmain = (LinearLayout) mcontext.findViewById(R.id.ln);		
		dataAdpater = new DataAdapter(mcontext);
		dataAdpater.OpenDataBase(); // open connect database
	
	//======================================================================================= 
		// Query Nguyenlieu or TheLoai,XuatXu
		if (column.equals("MaNguyenLieu")) {
			try {
				Cursor c = dataAdpater
						.QueryData("Select  M.TenMonAn,M.MaMonAn,M.rating,M.view from MonAn M "
								+ "Join MonAn_NguyenLieu  MN on M.MaMonAn = MN.MaMonAn "
								+ "Join NguyenLieu N on N.MaNguyenLieu = MN.MaNguyenLieu and N.MaNguyenLieu = "
								+ ma+ " "
								+ "Group by (M.TenMonAn) Having count(distinct N.MaNguyenLieu)=1");
				if (c != null) {
					if (c.moveToFirst()) {
						do {
							String Ten = c.getString(c
									.getColumnIndex("M.TenMonAn"));
							int Ma = c.getInt(c.getColumnIndex("M.MaMonAn"));
							float rating = Float.parseFloat(c.getString(c.getColumnIndex("M.rating")));
							String viewer = c.getString(c.getColumnIndex("M.view"));							
							array_list.add(new StringInPair(Ten, Ma,rating,viewer));
						} while (c.moveToNext());
					}
				}

			} catch (Exception ex) {
				Log.v("tag", "querry error");
			}
		} else {
			try {
				Cursor c = dataAdpater.QueryData("Select MaMonAn,TenMonAn,rating,view from MonAn where "
						+ column + "= '"
						+ ma + "'");
				if (c != null) {
					if (c.moveToFirst()) {
						do {
							String str = c.getString(c
									.getColumnIndex("TenMonAn"));
							int i = c.getInt(c.getColumnIndex("MaMonAn"));
							float rating = Float.parseFloat(c.getString(c.getColumnIndex("rating")));
							String viewer = c.getString(c.getColumnIndex("view"));							
							array_list.add(new StringInPair(str, i,rating,viewer));
						} while (c.moveToNext());
					}
				}

			} catch (Exception ex) {
				Log.v("tag", "querry error");
			}
		}
		dataAdpater.close(); // close conect database
		
		
		adapter = new MyArrayAdapter(mcontext, array_list, "MonAn");
		listview.setAdapter(adapter);
		txttitle = (TextView) view.findViewById(R.id.txttitle);
		txttitle.setText(loai);
		txtback = (TextView) view.findViewById(R.id.txtback);
		
		// click back 
		txtback.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Home home = new Home(mcontext);
				layoutmain.removeAllViews();
				layoutmain.addView(home.getView());
			}
		});		
		
	//======================================================================================= 
		// click item on list		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> list, View view,
					int position, long id) {
				StringInPair item = (StringInPair) list
						.getItemAtPosition(position);			
				
				Bundle bundle = new Bundle();
				bundle.putString("Ten", item.Loai);
				bundle.putInt("Ma", item.Ma);
				bundle.putInt("SELECT", 1);
				Intent intent = new Intent(mcontext, TextContent.class);
				intent.putExtras(bundle);
				mcontext.startActivity(intent);

			}
		});
		view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		return view;
	}

}
